home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Floppyshop 2
/
Floppyshop - 2.zip
/
Floppyshop - 2.iso
/
diskmags
/
0022-3.564
/
dmg-3313
/
news.txt
/
c_part3.asc
< prev
next >
Wrap
Text File
|
1989-04-05
|
12KB
|
389 lines
ALL AT C
Part 3 - Dave Mooney
In part 2 of this tutorial I covered how to print characters and numbers
to the screen and briefly touched on getting input from the user. Unfortu-
nately I did not go into this in as much detail as I would have liked due
to lack of time.
This time I will cover control loops and arithmetic operations and go into
more detail on how to input data to your programs.
CONTROL LOOPS:
==============
The control loops are:
FOR LOOP, these are roughly equivalent to the For...Next...Step loops used
in BASIC. The main difference is that it is possible to have
more than one starting, ending and step conditions.
WHILE LOOP, there are two types of while loop available in C. These
translate to the pre-conditioned loop (While...End While) and
the post-conditioned loop (Repeat...Until) available in Basic.
While it is often possible to any of the loop constructs in the same piece
of program, one will be more suited than the others.
FOR loop syntax
---------------
for(exp1;exp2;exp3)
{
statements
}
exp1 is the starting values for the variables used in the loop. If more
than one assignation is used they should be separated by commas.
exp2 while this condition is true the statements for the loop will be
executed. More than one condition may be set, each separated by
commas.
exp3 is the actions on the variables used in the loop. Again more that
one may be used and each separated by commas.
statements can be one or more program lines. If more than one line is
used they should be enclosed by braces {}.
While it is fairly easy to accept that more than one variable may be
initialised, be used for the exit conditions or updated on each iteration
it is a bit harder to accept that none of the conditions need to be set at
all. When this happens the loop will be 'endless'. What can be appropriate
though is that the initial conditions or the increments are set within the
body of the loop.
Simple : for(i=1;i<100;i++)
{
printf("%d\n",i);
}
This loop will start with i=1 and execute the statements in the braces
while i<100. i is incremented (i++) by one each time.
Multi assignment: for(i=1,j=2;j<50;i++,j+=2)
{
printf("%d %d\n",i,j);
}
Here the loop starts with i=1 and j=2. The statements are executed while
j<50. i is incremented by 1 and j by 2.
Some of each : for(i=10,j=20;j<100;)
{
printf("%d %d\n",i,j);
j=j+5;
}
This time the loop starts with i=10 and j=20. The end condition is j<100,
but there is no incremental part defined. The statements execute until
j=100 and the end condition (exp2) becomes false.
i and j are printed on each iteration of the loop and j is incremented by
5.
To try these out in a program you will need:
# include "stdio.h"
main()
{
int i,j;
for(i=32;i=127;i++) \* replace the various examples here *\
{
printf("%d\n",i);
}
j=getchar(); \* holds output on screen for examination *\
}
WHILE loop syntax
-----------------
while(exp1)
{
statements
}
exp1 is a condition that causes the loop to execute its associated
statements (when it is true) or terminate (when it is false).
statements can be one or more program lines. They are bound by braces {}
This version of the while loop is used when the values in exp1 are already
known. It is called a pre-conditioned loop and is similar to the WHILE...
WHILE END loop in BASIC.
If the condition of exp1 is true the statements will be executed until it
becomes false.
An example:
# include "stdio.h"
main() \* will print numbers 1 - 99 *\
{
int i;
i=1;
while(i<100)
{
printf("%d\n",i);
i++;
}
i=getchar();
}
DO...WHILE loop syntax
----------------------
do
{
statements
}
while(exp1);
statements can be one or more program lines. They are bound by braces {}
exp1 is a condition that causes the loop to re-execute its associated
statements (when it is true) or terminate (when it is false).
Note the semi colon after 'while(exp1)'.
This version of the while loop is used when the values in exp1 are un-
known. It is called a post-conditioned loop and is similar to the
REPEAT...UNTIL loop in BASIC.
The statements in the loop will always execute at least once. They will
continue to be executed until the condition of exp1 becomes false.
An example:
# include "stdio.h"
main() \* will print numbers 255 - 100 *\
{
int i,j;
j=255;
do
{
i=j;
printf("%d\n",i);
j=j-1;
}
while (i>100);
i=getchar();
}
ARITHMETIC EXPRESSIONS
======================
Up until now any operations on variables in the example programs have been
fairly simple. This is not because arithmetic or logic is any different or
difficult in C, but that it is represented slightly differently. What
follows is a list, broken down into groups. It is probably best just to
read it and then use it as a reference when they crop up in programs.
C has 45 operators organised in 9 groups. The operators in each have the
same precedence. The groups are listed in highest to lowest precedence and
associativity is given.
Group Operator Associativity Example
--------------------------------------------------------------------------
1 () function call left to right getchar(i)
[] array element reference i[17]
-> pointer to structure a->b
. structure member reference a.b
2 - unary minus left to right -a
++ increment a++
-- decrement a--
! logical negation (NOT) ! found
~ ones compliment ~Ox7f
* indirection * pointr
& address off &a
sizeof size of variable or type sizeof(a)
('type') cast (int) c
3 * multiplication left to right a*b
/ division a/b
% modulus a%b
4 + addition left to right a+b
- subtraction a-b
5 << bit shift left left to right a<<2
>> bit shift right a>>3
6 < less than left to right a<b
> greater than a>b
<= less than or equal to a<=b
>= greater than or equal to a>=b
7 == equal to left to right a==b
!= not equal to a!=b
8 & bitwise and left to right a&b
9 ^ bitwise exclusive or left to right a^b
10 | bitwise or left to right a|b
11 && logical and left to right a==4&&b++3
12 || logical or left to right a==3||b!=4
13 ?: conditional expression right to left a>?a:b
14 = assignment right to left a=5
*= multiply then assign a*=5
/= divide then assign a/=6
%= modulus divide then assign a%=5
+= add then assign a+=3
-= subtract then assign a-=4
&= bitwise and then assign a&=O333
^= bitwise exclusive or then assign a^=O123
|= bitwise or then assign a|=Ox77
<<= bitshift left then assign a<<=3
>>= bitshift right then assign a>>=2
15 , evaluate and then discard right to left a=4,b
MORE INPUT AND OUTPUT
=====================
So far we have only touched on input and output. An example of output that
we have used from the beginning is 'printf()', an example of input is
'getchar()'.
The getchar() function reads from the standard input device, ie the key-
board and returns the result in a variable. It also has an 'opposite
function, 'putchar(c), which takes the variable as its argument and sends
it to the standard output device, ie the screen.
Enter the following program to see these working:
# include "stdio.h"
main()
{
char c; /* the variable for input and output */
printf("Enter characters, 'Q' will finish program\n\n");
do /* start of DO...WHILE loop */
{
c=getchar(); /* get a key press */
putchar(c); /* print it on screen */
}
while (c != 'Q'); /* while its not equal to Q */
printf("\nOk, press a key to go to desktop.");
c=getchar();
}
If you entered this example you should have noticed two things.
1) The getchar() function echoes the key press to the screen in all cases
until the DO...WHILE conditions are false, ie 'Q' was pressed.
2) The function acts like the INKEY$ function in BASIC. It does not re-
quire the Enter key to be pressed. Unlike the INKEY$ function it will wait
until a key is pressed.
I'll finish this tutorial with a short program to print out a multiplica-
tion table between 2 and 9. It will use the arithmetic and logic expres-
sions given in this article as well as a FOR loop.
The only point to be aware of is that there is absolutely no error check-
ing and that pressing keys other than the numbers 2 - 9 will give create
funny tables. A copy of my C work disc to the first person who can tell me
why (I do know - really).
# include "stdio.h"
main()
{
int i,j,c; /* 3 integer variables */
do
{
printf("\nEnter a number between 1 and 10\n\n");
i=getchar(); /* get the keypress */
i=i-48; /* subtract 48 from ASCII*/
printf(" times table\n"); /* getchar() echoes to screen */
for (j=1;j<13;j++) /* loop between 1 and 12 */
{
printf("%d x %d = %d\n",i,j,i*j);
}
printf("\n\nPress any key to continue, 'Q' to quit");
c=getchar();
}
while (c != 'Q'); /* loop while c <> Q */
}
Don't be afraid to experiment and try out different ways of doing the same
thing. Only change one thing at a time so that you don't get confused with
more that one error. Always look for the obvious such as missing semi-
colons when your compiler throws a wobbly.
Dave Mooney
~~~OOOO~~~